define(function(require, exports, module) {
    'use strict';

    var rk = require('rk');

    $.fn.runMethod = function(fnName, a0, a1, a2, a3, a4, a5, a6, a7) {
        // var me = this.size() ? this.eq(0) : null,
        //     wtype = me.attr('wtype');
        // if ($.rk[wtype]) {
        //     var widget = me.data($.rk[wtype].prototype.widgetFullName);
        //     return widget && widget[fnName].apply(widget, $.makeArray(arguments).slice(1));
        // }
        var elem = $(this);
        var wtype = elem.attr('wtype');
        if (!wtype || !$.rk[wtype]) {
            // console.error('不存在控件类型' + wtype);
            return;
        }
        if (!elem.data('widgetInited')) return; //尚未初始化
        var arg = arguments;
        try {
            if (arg.length > 1) {
                return elem[wtype](fnName, a0, a1, a2, a3, a4, a5, a6, a7); //偷个懒喽
            } else {
                return elem[wtype](fnName);
            }
        } catch (e) {
            // console.error('执行' + wtype + '的' + fnName + '方法出错');
            return null;
        }
    };

    $.widget('rk.xsyBase', {
        options: {

        },
        isEditMode: function() {
            return SESSION.prj === 'canvas';
        },
        getType: function() {
            return this.element.attr('wtype')
        },
        getUid: function() {
            return this._getUid();
        },
        _getUid: function() {
            return $(this.element).attr('id');
        },
        getUuid: function() {
            return $(this.element).attr('id');
        },
        _getDetailId: function() {
            return rk.crm.getDetailId();
        },
        _getGroupId: function() {
            return rk.crm.getGroupId();
        },
        _getBelongId: function() {
            return rk.crm.getBelongId();
        },
        _getCommonData: function() {
            return rk.dataMgr.getCommonData();
        },
        _getLang: function() {
            return window.SESSION.user.language;
        },
        _getInfo: function() {
            var me = this;
            var uid = me.getUid();
            return rk.formMgr.getWidgetInfo(uid);
        },
        //初始化子节点
        _initChildren: function() {
            var me = this;
            var elem = me.element;

            elem.find('[wtype]').each(function() {
                var el = $(this);
                var wid = el.attr('id');
                var wtype = el.attr('wtype');
                rk.formMgr.createWidget(el);
            });
        },
        _listenDataEvent: function(key, callback) {
            var me = this;
            me._listenInnerEvent('on_' + key + '_change', callback);
        },
        _listenInnerEvent: function(eventName, callback) {
            var me = this;
            var elem = me.element;
            elem.off(eventName).on(eventName, function(e, data) {
                callback(data);
            });
        },
        _triggerInnerEvent: function(eventName, data) {
            var me = this;
            var elem = me.element;
            $(elem).trigger(eventName, data);
        },
        _listenOuterEvent: function(eventName, callback) {
            var me = this;
            var elem = me.element;
            var uid = me._getUid();
            rk.formMgr.addBroadcastListener(uid, eventName, callback);
        },
        _triggerOuterEvent: function(eventName, info) {
            var me = this;
            var elem = me.element;
            if (typeof info == 'undefined') info = {};
            info.__fromUid = me.getUid();
            rk.formMgr.broadcast(eventName, info);
        },
        triggerOuterEvent: function(eventName, info) {
            var me = this;
            return me._triggerOuterEvent(eventName, info);
        },
        _getDefaultInitHtml: function() {
            return require('platform/widgets/initing.tpl');
        },
        _beIniting: function(html) {
            var me = this;
            var elem = me.element;
            if (typeof html == 'undefined') html = rk.templateText(require('platform/widgets/initing.tpl'));
            elem.html(html);
            elem.find('div.xsy_su_refer_initing').fadeIn();
            elem.find('div.xsy_account_hierarchy_initing').fadeIn();
            elem.find('div.xsy_su_header_initing').fadeIn();
        },
        _beLoading: function() {
            var me = this;
            var elem = me.element;
            elem.beLoading({
                fixedPosition: true
            });
        },
        _unLoading: function() {
            var me = this;
            var elem = me.element;
            elem.unLoading();
        },
        _loadData: function(data, successFn, errorFn) {
            var me = this;
            var elem = me.element;

            if (elem.data('1st_round_data_loading_is_done')) me._beLoading(); //初始化后的数据绑定，出现loading

            var uid = me._getUid();
            var wtype = me.getType();
            rk.dataMgr.loadData(uid, data, function() {
                var args = arguments;
                var t1 = new Date();
                successFn.apply(me, args);
                me._unLoading();
                elem.data('1st_round_data_loading_is_done', true);
                //性能
                if (seajs.data.dev) {
                    var t2 = new Date();
                    var cost = (t2 - t1);
                    if (!rk.formMgr._wcost[uid]) rk.formMgr._wcost[uid] = {};
                    var initcost = rk.formMgr._wcost[uid].init;
                    initcost = initcost ? initcost : '';
                    rk.formMgr._wcost[uid].binddata = cost;

                    // console.log('%c ' + wtype + '#' + uid + ', init=' + initcost + 'ms' + ' bind=' + cost + 'ms', 'color:white;background-color:#6666ff;');
                }
            }, function() {
                var args = arguments;
                if ($.isFunction(errorFn)) errorFn.apply(me, args);
            });
        },
        _saveAttributes: function(attributes) { //向后台保存当前显示状态
            var me = this;
            var elem = me.element;

            var uid = me._getUid();
            rk.dataMgr.saveAttributes(uid, attributes, function() {

            }, function() {

            });
        },
        _getOwnerId: function() { //如果是内置控件，可以获得所在主人控件的数据
            var me = this;
            var elem = me.element;
            var ownerId;
            elem.parents('[id][wtype]').each(function(i, p) {
                var p = $(this);
                if ($.isNumeric(p.attr('id'))) {
                    ownerId = p.attr('id');
                    return false; //break;
                }
            });
            return ownerId;
        },
        _getOwnerData: function() { //如果是内置控件，可以获得所在主人控件的数据
            var me = this;
            var elem = me.element;
            var ownerId = me._getOwnerId();
            return rk.dataMgr.getWidgetData(ownerId)
        },
        bindData: function() {
            //绑定数据
        },
        _delegateMenu: function(config) {
            var me = this;
            var elem = me.element;
            config = $.extend(config, {
                container: elem
            });
            rk.delegateMenu(config);
        },
        isInited: function() {
            var me = this;
            var id = me._getUid();
            return rk.formMgr.isInited(id);
        },
        handleChildDeleted: function(){
            //子控件被移除时触发
        },
        handleChildAdded: function(){
            //子控件被移除时触发
        },
        handleAttributeChange: function(attributes){
            //设计器专属，属性改变后触发
        },
        handleChildrenChange: function(childreninfo){
            //设计器专属，子节点更改后触发
        }
    });
});
